#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash su2-6.0.1.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
yum install -y  environment-modules csh time patch libatomic perl-Data-Dumper autoconf automake libtool numactl binutils systemd-devel valgrind flex wget

set -e
yum install python2-numpy.aarch64 -y

cd $buildpath
wget -t 0 -c https://github.com/su2code/SU2/archive/refs/tags/v6.0.1.tar.gz -O SU2-6.0.1.tar.gz
tar -zxvf SU2-6.0.1.tar.gz && rm -rf SU2-6.0.1.tar.gz
cd SU2-6.0.1
./bootstrap
./configure --prefix=$installpath/SU2-6.0.1 CXXFLAGS="-O3 -std=c++11 -march=armv8.2-a -mtune=tsv110 -ffp-contract=off -fsigned-char" --enable-mpi --with-cc=mpicc --with-cxx=mpicxx --with-hdf5-lib=$installpath/hdf5-1.12.1/lib --with-hdf5-include=$installpath/hdf5-1.12.1/include
make -j$(nproc)
make -j$(nproc) install

cat>"$installpath/SU2-6.0.1/SU2_modulefiles"<<EOF
#%Module1.0
conflict su2
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set SU2 \$pwd
setenv SU2 \$SU2
set SU2_HOME \$SU2
setenv SU2_HOME \$SU2
set SU2_RUN \$SU2/bin
setenv SU2_RUN \$SU2/bin
prepend-path PATH \$SU2_RUN
prepend-path PYTHONPATH \$SU2_RUN
EOF

rm -rf $buildpath/SU2-6.0.1
